package com.rc.evcharger.service.log;

import com.rc.evcharger.common.log.ChargeProcessLogVo;
import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.mapper.order.OrderInfoMapper;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.model.order.OrderInfoExample;
import com.rc.evcharger.mongo.ChargedProcessLogDao;
import com.rc.evcharger.vo.log.ChargedProcessLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Create by Yang on 2019/11/20
 */
@Service
public class ChargedProcessLogServiceImpl implements ChargedProcessLogService {

    private static Logger logger = LogManager.getLogger(ChargedProcessLogServiceImpl.class);
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");//时间格式要与mongodb对应

    @Autowired
    private ChargedProcessLogDao chargedProcessLogDao;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public void addChargedProcessLog(ChargedProcessLog log) {
        chargedProcessLogDao.insert(log, log.getCollectionName());
        logger.info("addChargedProcessLog mongodb 创建成功,:{},:{}", log.getCollectionName(), log.getOrderNum());
    }

    @Override
    public PageImpl<ChargedProcessLog> findPageBySearch(ChargeProcessLogVo search) {
        //查找订单信息
        OrderInfoExample example = new OrderInfoExample();

        Query query = new Query();
        if (StringUtils.isNotBlank(search.getOrderNum())) {
            example.createCriteria().andOrderNumEqualTo(search.getOrderNum());

            query.addCriteria(Criteria.where("orderNum").is(search.getOrderNum()));
        } else if (StringUtils.isNotBlank(search.getOutOrderNum())) {
            example.createCriteria().andOutOrderNumEqualTo(search.getOutOrderNum());

            query.addCriteria(Criteria.where("outOrderNum").is(search.getOutOrderNum()));
        }

        Sort sort = null;
        //倒序
        if (search.getOrderBy() == 1) {
            sort = Sort.by(Sort.Direction.DESC, "createTime");
        } else {
            sort = Sort.by(Sort.Direction.ASC, "createTime");
        }


        List<OrderInfo> list = orderInfoMapper.selectByExample(example);
        if (list != null && list.size() > 0) {

            //得到开始时间
            OrderInfo orderInfo = list.get(0);
            String startTime = DateHelper.dataToString(orderInfo.getStartTime(), DateHelper.DATE_FORMAT_YYYY_MM_DD);


            ChargedProcessLog chargeProcessLog = new ChargedProcessLog(startTime);


            PageImpl<ChargedProcessLog> pages = chargedProcessLogDao.findPage(query, sort, search.getOffset(), search.getLimit(), chargeProcessLog.getCollectionName());

            return pages;
        } else {
            return null;
        }
    }
}
